﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace QuestionsAnswers.UI.Models
{
    public class Course
    {
        public Course()
        {
            this.NumberofRequiredQuestionsPerUser = 0;
            Owners = new List<User>();
            Participants = new List<User>();
        }

        public int CourseId { get; set; }
        public string Name { get; set; }
        public int NumberofRequiredQuestionsPerUser { get; set; }
        // public int CreatorId { get; set; }

        [InverseProperty("Created")]
        public virtual User Creator { get; set; }

        [InverseProperty("Owning")]
        public virtual List<User> Owners { get; set; }

        [InverseProperty("Participating")]
        public virtual List<User> Participants { get; set; }

        public virtual List<Question> Questions { get; set; }

        public virtual List<AssignedToUser> AssignedToUsers { get; set; }

        public virtual List<Invitation> Invitations { get; set; }



        public List<Invitation> GetInvitations(String email, string memberType, User inviter)
        {
            if (email == null || memberType == null || inviter == null)
            {
                throw new ArgumentNullException();
            }
            if (email == string.Empty)
            {
                return new List<Invitation>();
            }
            return email.Split(new[] { Environment.NewLine }, StringSplitOptions.None).Select(s => new Invitation
            {

                Course = this,
                InvitedEmail = s,
                Inviter = inviter,
                MemberType = memberType
            }).ToList();
        }


        public bool CanModify(String username)
        {
            return Creator.UserName == username || (Owners != null && Owners.Exists(x => x.UserName == username));
        }

        public List<User> GetAllUsers()
        {
            var users = new List<User>();
            users.AddRange(Participants);
            users.AddRange(Owners);
            users.Add(Creator);
            return users;
        }
        public override bool Equals(object that)
        {
            if (that == this)
                return true;
            Course thatCourse = that as Course;
            if ((object)thatCourse == null)
            {
                return false;
            }
            return this.CourseId == thatCourse.CourseId;
        }
    }


    public class Invitation
    {
        public int InvitationId { get; set; }
        public int CourseId { get; set; }
        public virtual Course Course { get; set; }


        public String InvitedEmail { get; set; }
        public String MemberType { get; set; }
        [InverseProperty("Invited")]
        public virtual User Inviter { get; set; }

    }

    public class AssignedToUser
    {
        public int AssignedToUserId { get; set; }
        public int UserId { get; set; }
        public virtual User User { get; set; }
        public int CourseId { get; set; }
        public virtual Course Course { get; set; }
        public int QuestionId { get; set; }
        public virtual Question Question { get; set; }
    }

    public class CourseAssignViewModel
    {
        public virtual Course Course { get; set; }
        public bool Selected { get; set; }
        public List<UserCourseAssignViewModel> Users { get; set; }
    }

    public class UserCourseAssignViewModel
    {
        public String Value { get; set; }
        public bool Selected { get; set; }
        public String Text { get; set; }
        public int NumberOfQuestionAskedForCourse { get; set; }

        public int NumberOfQuestionAssignedForCourse { get; set; }
    }

    //public class UserAssignViewModel
    //{
    //    public int UserId { get; set; }
    //    public User User { get; set; }
    //    public Selected
    //}

    public class CoursePass
    {
        public List<User> Users { get; set; }
        public Course Course { get; set; }
        public bool Editable { get; set; }
        public String MemberType { get; set; }
    }

    public class CourseMember
    {
        public User User { get; set; }
        public Course Course { get; set; }
        public String MemberType { get; set; }
    }
    public class CourseMemberIds
    {
        public int UserId { get; set; }
        public int CourseId { get; set; }
        public String MemberType { get; set; }
    }


}
